package com.love.travel.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.love.travel.mapper.TravelProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.love.common.log.annotation.Log;
import com.love.common.log.enums.BusinessType;
import com.love.common.security.annotation.RequiresPermissions;
import com.love.travel.domain.TravelProduct;
import com.love.travel.service.ITravelProductService;
import com.love.common.core.web.controller.BaseController;
import com.love.common.core.web.domain.AjaxResult;
import com.love.common.core.utils.poi.ExcelUtil;
import com.love.common.core.web.page.TableDataInfo;

/**
 * 旅游产品Controller
 * 
 * @author love
 * @date 2025-10-22
 */
@RestController
@RequestMapping("/product")
public class TravelProductController extends BaseController
{
    @Autowired
    private ITravelProductService travelProductService;
    @Autowired
    private TravelProductMapper travelProductMapper;

    /**
     * 查询旅游产品列表
     */
    @RequiresPermissions("travel:product:list")
    @GetMapping("/list")
    public TableDataInfo list(TravelProduct travelProduct)
    {
        startPage();
        List<TravelProduct> list = travelProductService.selectTravelProductList(travelProduct);
        return getDataTable(list);
    }

    /**
     * 导出旅游产品列表
     */
    @RequiresPermissions("travel:product:export")
    @Log(title = "旅游产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TravelProduct travelProduct)
    {
        List<TravelProduct> list = travelProductService.selectTravelProductList(travelProduct);
        ExcelUtil<TravelProduct> util = new ExcelUtil<TravelProduct>(TravelProduct.class);
        util.exportExcel(response, list, "旅游产品数据");
    }

    /**
     * 获取旅游产品详细信息
     */
    @RequiresPermissions("travel:product:query")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId)
    {
        return success(travelProductService.selectTravelProductByProductId(productId));
    }

    @GetMapping("/listByUserid")
    public TableDataInfo listByUserid()
    {
        List<TravelProduct> list = travelProductService.selectTravelProductByUserId();
        return getDataTable(list);
    }

    /**
     * 新增旅游产品
     */
    @RequiresPermissions("travel:product:add")
    @Log(title = "旅游产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TravelProduct travelProduct)
    {
        return toAjax(travelProductService.insertTravelProduct(travelProduct));
    }

    /**
     * 修改旅游产品
     */
    @RequiresPermissions("travel:product:edit")
    @Log(title = "旅游产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TravelProduct travelProduct)
    {
        return toAjax(travelProductService.updateTravelProduct(travelProduct));
    }

    /**
     * 删除旅游产品
     */
    @RequiresPermissions("travel:product:remove")
    @Log(title = "旅游产品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {
        return toAjax(travelProductService.deleteTravelProductByProductIds(productIds));
    }

    /**
     * 查询产品总数用于生成前端编码
     */
    @GetMapping(value = "/getProductTotal")
    public AjaxResult getProductTotal() {
        Integer totalNum = this.travelProductMapper.selectProductTotal();
        return AjaxResult.success(totalNum);
    }

    /**
     * 远程获取城市级联数据
     * @return
     */
    @GetMapping(value = "/cascader")
    public AjaxResult cascader() {
        AjaxResult result = travelProductService.selectCitys();
        List<Object> citys = (List<Object>) result.get(AjaxResult.DATA_TAG);
        return AjaxResult.success(citys);
    }
}
